﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using eProject.Model;
using eProject.ViewModel;

namespace eProject.View
{
    /// <summary>
    /// Interaction logic for ScheduleSettingsDialog.xaml
    /// </summary>
    public partial class ScheduleSettingsDialog
    {
        private readonly CommonManager _manager;

        public ScheduleSettingsDialog(CommonManager manager)
        {
            if (manager == null) throw new ArgumentNullException("manager");
            _manager = manager;

            InitializeComponent();
            DataContext = this;
            ScheduleItems = new ObservableCollection<ScheduleItemViewModel>(_manager.ScheduleItems);

            column1.Width = Properties.Settings.Default.ScheduleSettingsDialogColumnWidth1;
            column2.Width = Properties.Settings.Default.ScheduleSettingsDialogColumnWidth2;
            column3.Width = Properties.Settings.Default.ScheduleSettingsDialogColumnWidth3;            
            Closed += delegate
                          {
                              Properties.Settings.Default.ScheduleSettingsDialogColumnWidth1 = column1.Width;
                              Properties.Settings.Default.ScheduleSettingsDialogColumnWidth2 = column2.Width;
                              Properties.Settings.Default.ScheduleSettingsDialogColumnWidth3 = column3.Width;
                          };
        }

        public bool Monday { get; set; }
        public bool Tuesday { get; set; }
        public bool Wednesday { get; set; }
        public bool Thursday { get; set; }
        public bool Friday { get; set; }
        public bool Satureday { get; set; }
        public bool Sunday { get; set; }

        public double MondayLenght{ get; set;}
        public double TuesdayLength { get; set; }
        public double WednesdayLength { get; set; }
        public double ThursdayLength { get; set; }
        public double FridayLength { get; set; }
        public double SaturedayLength { get; set; }
        public double SundayLength { get; set; }

        public ObservableCollection<ScheduleItemViewModel> ScheduleItems{ get; private set;}        

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void CommandBinding_CreateExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var dialog = new CreateScheduleItemDialog(_manager){Owner = this};
            var result = dialog.ShowDialog();
            if (result == true)
            {
                var empls = dialog.everyoneCheckbox.IsChecked == true?new List<EmployeeViewModel>(0):dialog.Employees;
                var mh = dialog.ManHours;
                var period = dialog.Period;
                ScheduleItems.Add(new ScheduleItemViewModel(empls,mh, period));
            }
        }

        private void CommandBinding_DeleteExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var items = scheduleItemsListView.SelectedItems.Cast<ScheduleItemViewModel>().ToList();
            foreach (var item in items)
            {
                ScheduleItems.Remove(item);
            }
        }

        private void CommandBinding_DeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = scheduleItemsListView.SelectedItem as ScheduleItemViewModel != null;
        }

        private void CommandBinding_MoveUpCommand(object sender, ExecutedRoutedEventArgs e)
        {
            var index = scheduleItemsListView.SelectedIndex;
            ScheduleItems.Move(index,index - 1);
        }

        private void CommandBinding_MoveUpCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (scheduleItemsListView == null) return;
            var index = scheduleItemsListView.SelectedIndex;
            e.CanExecute = index > 0;
        }

        private void CommandBinding_MoveDownCommand(object sender, ExecutedRoutedEventArgs e)
        {
            var index = scheduleItemsListView.SelectedIndex;            
            ScheduleItems.Move(index, index + 1);
        }

        private void CommandBinding_MoveDownCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (scheduleItemsListView == null) return;
            var index = scheduleItemsListView.SelectedIndex;
            e.CanExecute = index < scheduleItemsListView.Items.Count - 1 && index >= 0;
        }
    }

    public class PeriodToTextConverter : IValueConverter
    {
        public static readonly PeriodToTextConverter Instance = new PeriodToTextConverter();


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var period = value as Period;
            if (period == null) return null;
            var cult = System.Threading.Thread.CurrentThread.CurrentCulture;
            var begin = period.Begin.ToString(cult.DateTimeFormat.ShortDatePattern);
            var end = period.End.ToString(cult.DateTimeFormat.ShortDatePattern);
            return begin + " - " + end + "  ";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    
}
